package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.ConsigneeMapper;
import com.gtq.uhome.mapper.OrderDetailsMapper;
import com.gtq.uhome.mapper.OrderMapper;
import com.gtq.uhome.mapper.UserMapper;
import com.gtq.uhome.service.MyUhomeService;

import com.gtq.uhome.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("myUhomeService")
public class MyUhomeServiceImpl implements MyUhomeService {
    @Resource
    ConsigneeMapper consigneeMapper=null;
    @Resource
    UserMapper userMapper=null;
    @Resource
    OrderDetailsMapper orderDetailsMapper=null;

    //更改收件人信息
    @Override
    public Integer update(UpdateConsigneeVo updateConsignee) {
        Integer update = consigneeMapper.updateConsignee(updateConsignee);
        return update;
    }


        //查询用户订单状态
    @Override
    public List<DeliveryOrderStatusVo> selectOrderSatus(SignInStatusVo signInStatus) {
        List<DeliveryOrderStatusVo> deliveryOrderStatuses = consigneeMapper.selectOrderSatus(signInStatus);

        return deliveryOrderStatuses;
    }
    //我的申请
    @Override
    public List<MyApplicationVo> selectMyApplication(ConditionQueryVo conditionQuery) {
        List<MyApplicationVo> myApplications = userMapper.selectMyApplication(conditionQuery);
        return myApplications;
    }





    //查询收货人信息
    @Override
    public List<ShippingOrderVo> selectShippingOrder(String orderId, String signStatus) {
        List<ShippingOrderVo> shippingOrders = orderDetailsMapper.selectShippingOrder(orderId, signStatus);
        return shippingOrders;
    }

    //查询生鲜
    @Override
    public Map<String, Object> selectShippingOrdersFresh(String orderId,String orderStatus) {
        List<ShippingOrders> shippingOrders = orderDetailsMapper.selectShippingOrdersFresh(orderId,orderStatus);
        int cout=0;
        int heavy=0;
        int price=0;
        for (int i = 0; i < shippingOrders.size(); i++) {
            ShippingOrders shipping = shippingOrders.get(i);
            cout+=shipping.getCount();
            heavy+=shipping.getHeavy();
            price+=shipping.getPrice();
        }
        Map<String,Object> map=new HashMap();
        map.put("fresh",shippingOrders);
        map.put("supCount",cout);
        map.put("heavy",heavy);
        map.put("price",price);
        return map;

    }
    //查询常温
    @Override
    public Map<String, Object> selectShippingOrdersTemperature(String orderId,String orderStatus) {
        List<ShippingOrders> shippingOrders1 = orderDetailsMapper.selectShippingOrdersTemperature(orderId,orderStatus);
        int cout1=0;
        int heavy1=0;
        int price1=0;
        for (int i = 0; i < shippingOrders1.size(); i++) {
        ShippingOrders shippingOrders = shippingOrders1.get(i);
            cout1+=shippingOrders.getCount();
            heavy1+=shippingOrders.getHeavy();
            price1+=shippingOrders.getPrice();
        }
        Map<String,Object> map=new HashMap();
        map.put("Temperature",shippingOrders1);
        map.put("supCount",cout1);
        map.put("heavy",heavy1);
        map.put("price",price1);
        return map;

    }

    //修改订单状态:用户是否签收成功
    @Override
    public Integer updateOrderState(SignInStatusVo signInStatus) {
        Integer integer = consigneeMapper.updateOrderState(signInStatus);
        return integer;
    }
    //添加订单完成后的时间(订单完成时间)
    @Override
    public Integer updateOrderTime(SignInStatusVo signInStatus) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        Timestamp timestamp = Timestamp.valueOf(format);
        signInStatus.setPickuPtime(timestamp);
        Integer integer = consigneeMapper.updateOrderTime(signInStatus);
        return integer;
    }
        //配送员接单更改订单状态
    @Override
    public Integer updateOrder(SignInStatusVo signInStatus) {
        Integer integer = consigneeMapper.updateOrder(signInStatus);
        return integer;
    }
        //订单退单
    @Override
    public Integer updateOrderBack(SignInStatusVo signInStatus) {
        return consigneeMapper.updateOrderBack(signInStatus);
    }

    //配送员消息插入
    @Override
    @Transactional
    public Integer insertMsg(SignInStatusVo signInStatus) {
        Integer rersult=0;

        //查询订单状态
       if (signInStatus.getOrderStatus().equals("签收成功")) {
           signInStatus.setMsg("亲爱的配送员，您订单号为"+signInStatus.getOrderId()+"的订单已签收，订单情况："+signInStatus.getSignIn()+"，签收时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
           signInStatus.setUserMsg("尊敬的用户，您订单号为"+signInStatus.getOrderId()+"的订单已签收，订单情况："+signInStatus.getSignIn()+"，签收时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +"，请您确认货物是否送达，若有问题请联系客服。");

       }else if(signInStatus.getOrderStatus().equals("签收失败")){
           signInStatus.setMsg("亲爱的配送员，您订单号为"+signInStatus.getOrderId()+"的订单签收失败，订单情况："+signInStatus.getSignIn()+"，签收时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
           signInStatus.setUserMsg("尊敬的用户，您订单号为"+signInStatus.getOrderId()+"的订单签收失败，订单情况："+signInStatus.getSignIn()+"，签收时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +",若有问题请联系客服。");

       }else if(signInStatus.getOrderStatus().equals("待取货")){
           signInStatus.setMsg("亲爱的配送员，您订单号为"+signInStatus.getOrderId()+"的订单正在退回中，退回时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +",若有问题请联系客服。");
           signInStatus.setUserMsg("尊敬的用户，您订单号为"+signInStatus.getOrderId()+"的订单正在退回中，退回时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) +",若有问题请联系客服。");

       }else if(signInStatus.getOrderStatus().equals("待送货")){
           signInStatus.setMsg("亲爱的配送员，您订单号为"+signInStatus.getOrderId()+"的订单取货成功，取货时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
           signInStatus.setUserMsg("尊敬的用户，您订单号为"+signInStatus.getOrderId()+"的订单取货成功，取货时间为："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

       }
        Integer staffMsg = consigneeMapper.insertStaffMsg(signInStatus);
       if (staffMsg==1){
        Integer userMsg = consigneeMapper.insertUserMsg(signInStatus);
        if (userMsg==1){
            rersult=1;
        }
       }
        return rersult;
    }
         /*   //用户消息插入
    @Override
    public Integer insertUserMsg(SignInStatusVo signInStatus) {
        Integer integer = consigneeMapper.inserUserftMsg(signInStatus);

        return integer;
    }*/

}
